package com.alibaba.citrus.ots.unified.inventory.utils;

import com.epoch.app.otsunifiedinventory.dto.response.AllocateOrderDetailsQueryResponse;
import com.epoch.app.otsunifiedinventory.dto.response.AllocateOrderMessageResponse;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author zhangguoqing.zgq
 * @date 2021/10/17
 */
public class AllocateMsgUtils {
    private static Log log = LogFactory.getLog(AllocateMsgUtils.class);

    /**
     * @param body
     * @return
     */
    public static AllocateOrderMessageResponse convert2AllocateOrder(byte[] body) {
        String messageBody = new String(body, StandardCharsets.UTF_8);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

        try {
            return (AllocateOrderMessageResponse)objectMapper.readValue(messageBody, AllocateOrderMessageResponse.class);
        } catch (Throwable e) {
            log.error("convert bytes to AllocateOrderMessageResponse exception!", e);
            return null;
        }
    }
}
